# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return root
        q = [[root]]
        result = []
        while q:
            node_list = q.pop(0)
            if not node_list:
                continue
            tmp_result = []
            tmp_leaf = []
            for node in node_list:
                if not node:
                    continue
                tmp_result.append(node.val)
                if node.left:
                    tmp_leaf.append(node.left)
                if node.right:
                    tmp_leaf.append(node.right)
            q.append(tmp_leaf)
            result.append(tmp_result)
        return result
